package com.fesine.ssh.service.impl;

import com.fesine.ssh.dao.SaleItemDao;
import com.fesine.ssh.dao.SalesOrderDao;
import com.fesine.ssh.model.SalesOrder;
import com.fesine.ssh.service.SalesOrderService;
import com.fesine.ssh.util.ShopException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单服务类实现
 * Created by Fesine on 2016/2/23.
 */
@Service("salesOrderService")
public class SalesOrderServiceImpl implements SalesOrderService {

    @Autowired
    private SalesOrderDao salesOrderDao;

    @Autowired
    private SaleItemDao saleItemDao;


    @Override
    public String add(SalesOrder salesOrder) {
        String id = salesOrderDao.save(salesOrder);
        //Set<SaleItem> saleItemSet = salesOrder.getSaleItems();
        //for (SaleItem saleItem : saleItemSet) {
        //    saleItemDao.save(saleItem);
        //}
        return id;
    }

    @Override
    public void update(SalesOrder salesOrder) {
        salesOrderDao.update(salesOrder);
    }

    @Override
    public void delete(String[] orderIds) {
        //将nullify字段改为false，作废订单
        for (String orderId : orderIds) {
            try {
                salesOrderDao.updateStatusByOrderId(orderId,null,0);
            } catch (ShopException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void verify(String[] orderIds) {
        //将status字段改为true，审核订单
        for (String orderId : orderIds) {
            try {
                salesOrderDao.updateStatusByOrderId(orderId, 1, null);
            } catch (ShopException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<SalesOrder> findAll() {
        return salesOrderDao.findAll();
    }

    @Override
    public List<SalesOrder> findAll(int offset, int pageSize) {
        return salesOrderDao.findAll(offset, pageSize);
    }

    @Override
    public SalesOrder findByOrderId(String orderId) {
        return salesOrderDao.findByOrderId(orderId);
    }
}
